package icu.zqyhdm.result;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;

/**
 * 全局统一返回结果类
 */
@Data
@ApiModel(value = "全局统一返回结果")
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "状态")
    private boolean flag;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    public Result(){}

    public static <T> Result<T> build(boolean flag,T data,ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        result.setFlag(flag);
        result.setCode(resultCodeEnum.SUCCESS.getCode());
        result.setMessage(resultCodeEnum.SUCCESS.getMessage());
        result.setData(data);
        return result;
    }
    public static <T> Result<T> build(boolean flag,ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();
        result.setFlag(flag);
        result.setCode(resultCodeEnum.SUCCESS.getCode());
        result.setMessage(resultCodeEnum.SUCCESS.getMessage());
        result.setData(null);
        return result;
    }

    /**
     * 操作成功
     */
    public static Result ok(){
        return Result.build(true,null,ResultCodeEnum.SUCCESS);
    }

    public static<T> Result<T> ok(T data){
        return Result.build(true,data,ResultCodeEnum.SUCCESS);
    }

    /**
     * 操作失败
     */
    public static Result fail(){
        return Result.build(false,null,ResultCodeEnum.FAIL);
    }

    public static Result fail(Integer code,String message){
        Result result = new Result<>();
        result.setFlag(false);
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return result;
    }
}
